package com.hpe.wx.performance.redis.task;

import java.io.IOException;
import java.util.Set;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MalformedObjectNameException;
import javax.management.ReflectionException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hpe.wx.performance.redis.service.PerFormanceService;

import redis.clients.jedis.Tuple;

@Component
public class GetJMXData {
	private static Logger logger = Logger.getLogger(GetJMXData.class.getSimpleName());
	@Autowired
	private PerFormanceService service;

	// @Scheduled(cron="0/5 * * * * ? ")
	// @Scheduled(fixedRate = 1000 * 5)
	public void taskCycle() {
		System.out.println("=========================================a");
	}

	/**
	 * heartbeat, it will execute per 30 seconds, to get data from jmx and store
	 * into redis if there is not any jmx or just for test, comment it
	 */
	// @Scheduled(fixedRate = 1000 * 15)
	public void getJMXData() {
		Set<Tuple> urls = service.getAllJMXURLs();
		for (Tuple tup : urls) {

			String elememt = tup.getElement();
			String[] jmxUrlandName = elememt.split("#");
			String jmxURL = jmxUrlandName[0];
			logger.info("The jmx for get info from remote machine is: " + jmxURL);
			try {
				service.addDate2Redis(jmxURL);
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}

		}
	}

}
